home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lsof_3.37 / scripts / count_pf.perl5 < prev    next >
Encoding:
Text File  |  1995-07-31  |  1.2 KB  |  63 lines

  1. #!/usr/local/bin/perl5
  2. #
  3. # count_pf.perl5 -- run lsof in repeat mode and count processes and
  4. #            files
  5.  
  6. sub interrupt { print "\n"; exit 0; }
  7.  
  8. $LSOF = "../lsof";            # path to lsof
  9. $RPT = 15;                # lsof repeat time
  10.  
  11. if ( ! -x $LSOF) { print "can't execute $LSOF\n"; exit 1 }
  12.  
  13. # Read lsof -HPF0 output repeatedly from a pipe.
  14.  
  15. $| = 1;                    # unbuffer output
  16. $SIG{'INT'} = 'interrupt';        # catch interrupt
  17. $proc = $files = $tcp = $udp = 0;
  18. $progress="/";
  19. open(P, "$LSOF -HPF0 -r $RPT|") || die "can't open pipe to $LSOF\n";
  20.  
  21. LSOF_LINE:
  22.  
  23. while (<P>) {
  24.     chop;
  25.     if (/^m/) {
  26.  
  27.     # A marker line signals the end of an lsof repetition.
  28.  
  29.     printf "%s  Processes: %5d,  Files: %6d,  TCP: %6d, UDP: %6d\r",
  30.         $progress, $proc, $files, $tcp, $udp;
  31.     $proc = $files = $tcp = $udp = 0;
  32.     if ($progress eq "/") { $progress = "\\"; } else { $progress = "/"; }
  33.     next LSOF_LINE;
  34.     }
  35.     if (/^p/) {
  36.  
  37.     # Count process.
  38.  
  39.     $proc++;
  40.     next LSOF_LINE;
  41.     }
  42.     if (/^f/) {
  43.  
  44.     # Count files.
  45.  
  46.     $files++;
  47.     @F = split("\0", $_, 999);
  48.     foreach $i (0 .. ($#F - 1)) {
  49.  
  50.     # Search for protocol field.
  51.  
  52.         if ($F[$i] =~ /^P(.*)/) {
  53.  
  54.         # Count instances of TCP and UDP protocols.
  55.  
  56.         if ($1 eq "TCP") { $tcp++; }
  57.         elsif ($1 eq "UDP") { $udp++; }
  58.         next LSOF_LINE;
  59.         }
  60.     }
  61.     }
  62. }
  63.